/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.web.mail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Usuario
 */
public class Mail {
    
    public void enviarMail(String destino, String pin){
        try
        {
            // Propiedades de la conexión
            Properties props = new Properties();
            props.setProperty("mail.smtp.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.starttls.enable", "true");
            props.setProperty("mail.smtp.port", "587");
            props.setProperty("mail.smtp.user", "homebankweb@gmail.com");
            props.setProperty("mail.smtp.auth", "true");

            // Preparamos la sesion
            Session session = Session.getDefaultInstance(props);

            // Construimos el mensaje
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("homebankweb@gmail.com"));
            message.addRecipient(
                Message.RecipientType.TO,
                new InternetAddress(destino));
            message.setSubject("Información del banco");
            message.setText("Le informamos que su pin es: "+pin);

            // Lo enviamos.
            Transport t = session.getTransport("smtp");
            t.connect("homebankweb@gmail.com", "banco123");
            t.sendMessage(message, message.getAllRecipients());

            // Cierre.
            t.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
}
